<!DOCTYPE HTML>
<html  xmlns:th="http://www.thymeleaf.org">
<head th:include="_meta :: header">
    <title>添加</title>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form form form-horizontal"  id="form-video-add" action="#">

            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>角色：</label>
                <div class="layui-input-inline">
                    <select type="layui-input"    class="layui-input"  name="role"  id="role"   lay-filter="roleTest" >
                        <option th:each="role:${roleList}" th:if="${role}" th:text="${role.name}" th:value="${role.id}" ></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户</label>
                <div class="layui-input-inline">
                    <select type="layui-input" class="layui-input" id="userName" name="userName"  lay-filter="userNameTest" >
                        <option value="0">--请选择用户--</option>
                    </select>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">下属</label>
                <div class="layui-input-block" id="xiashuId">
                    <input type="checkbox" name="xiashu" title="暂无" disabled>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <a  href="javascript:;" class="layui-btn" onclick="addByttonAll('/admin/EveryRoleController/updataMessage')">
                    确定添加
                </a>
                <button  class="layui-btn" id="reset" type="reset" >
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<div th:replace="_footer :: footerjs"></div>
<div th:replace="_ueditor :: ueditor"></div>
<!--/_footer 作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->

<link rel="stylesheet" type="text/css" th:href="@{/xadmin/lib/formSelect/formSelects-v4.css}"/>
<script type="text/javascript" th:src="@{/xadmin/lib/formselect/formSelects-v4.js}"></script>
<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/vendor/jquery.ui.widget.js}"></script>
<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/jquery.iframe-transport.js}"></script>
<script th:src="@{/xadmin/lib/jQuery-File-Upload/js/jquery.fileupload.js}"></script>



<script>

    layui.use(['form', 'layer','laydate'],function() {

        $ = layui.jquery;
        var form = layui.form,
            laydate = layui.laydate,
            formSelects = layui.formSelects,
            layer = layui.layer;
        lay('.timeSpace').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });

        // 这是第一个下拉框
        form.on('select(roleTest)', function(){
            var roleId = $("#role").val();
            $.ajax({
                type: "post",//请求方式get/post
                url: "/admin/EveryRoleController/findUserByRoldId",//请求对应的地址
                data: {"roleId": roleId},//往服务器传递的参数，
                dataType:"JSON",
                success: function (data) {//服务器交互成功调用的回调函数，data就是服务器端传递出来的数据
                    var userName = $("#userName");
                    var xiashuId=$("#xiashuId");
                    userName.empty();
                    xiashuId.empty();
                    xiashuId.append("<input  type=\"checkbox\"  name='xiashu' title=\'暂无\' disabled>");
                    var d =data.data;
                    if (d != null){
                        console.log("数据不为空=="+d.length);
                        for(var i =0 ;i < d.length;i++){
                            console.log("数据不为空=="+d[i].username);
                            userName.append("<option  value=\'"+d[i].id+"\'>"+d[i].username+"</option>");
                        }
                    }
                    form.render();
                },
                error: function () {
                    alert("没有对应的角色人物");
                }
            });
        });

        // 这是第二个复选框
        form.on('select(userNameTest)', function(){

            var roleId = $("#role").val();
            var userNameId = $("#userName").val();
            console.log("roleId==============="+roleId);
            console.log("userNameId==============="+userNameId)
            $.ajax({
                type: "post",//请求方式get/post
                url: "/admin/EveryRoleController/deitCheck",//请求对应的地址
                data: {"roleId": roleId,
                        "userId":userNameId},//往服务器传递的参数，
                dataType:"JSON",
                success: function (data) {//服务器交互成功调用的回调函数，data就是服务器端传递出来的数据
                    var xiashuId = $("#xiashuId");
                    var d =data.data;
                    if (d != null){

                        for(var k =0 ;k < d[1].length;k++){
                            xiashuId.empty();
                        }
                        for(var p =0 ;p < d[2].length;p++){
                            xiashuId.empty();
                        }

                        for(var i1 =0 ;i1 < d[2].length;i1++){
                            xiashuId.append("<input checked type=\"checkbox\" name='xiashu' title=\'"+d[2][i1].username+"\' value=\'"+d[2][i1].id+"\'>");

                        }
                        console.log(d);
                        for(var i =0 ;i < d[1].length;i++){
                            xiashuId.append("<input type=\"checkbox\" name='xiashu' title=\'"+d[1][i].username+"\' value=\'"+d[1][i].id+"\'>");
                        }

                    }
                    form.render();
                },
                error: function () {
                    alert("没有对应的角色人物");
                }
            });
        });


    });

    /**
     * 添加
     * @param url
     * @returns {boolean}
     */
    function addByttonAll(url){
        var _list = new Array();
        var  roleId=$('#role').val();
        var userName=$('#userName').val();
        url=url+"?roleId="+roleId+"&userNameId="+userName;

        $('input[name="xiashu"]:checked').each(
            function (i) {
                _list[i] = $(this).val();
            });
        if(_list.length == 0){
            console.log("_list.length"+_list.length);
            console.log("userName"+userName);
            layer.msg("数据不可以为空");
            return false;
        }
        layer.confirm('确定要添加选中的全部数据吗？', {
            btn : [ '确定', '取消' ]
            // 按钮
        }, function() {
            $.lock();
            $.ajax({
                url:url,// 跳转到 action
                data:JSON.stringify(_list),
                type:'post',
                cache:false,
                dataType:'json',
                contentType:"application/json",  //发送信息至服务器时内容编码类型。
                success:function(data) {
                    if(data.code == "1"){
                        var index = parent.layer.getFrameIndex(window.name);
                        layer.msg('编辑成功!',{icon:1,time:1000,end:function() {
                                parent.layer.close(index);
                            }});
                    }else{
                        $.unlock();
                        layer.msg("编辑失败");

                    }
                },
                error:function() {
                    $.unlock();
                    layer.msg("编辑异常",5);

                },
            });
        }, function() {

        });
    }

</script>


</body>
</html>